<template>
  <div class="chat-group-right">
    <div class="user">
      <div class="avatar">
        <canvas
          v-if="selectItem.chatId"
          :key="0"
          :ref="(el) => getDivDom(el, selectItem)"
          :style="{ background: ColorMap[index % 4] }"
        ></canvas>
        <div
          v-else
          style="width: 40px; height: 40px; border-radius: 50%; background-color: #ececec"
        ></div>
      </div>
      <div class="name">{{
        selectItem.name || enterpriseMembers?.map((item) => item.name).join(',')
      }}</div>
    </div>
    <div class="user-info">
      <div class="item">
        <div class="title">群公告:</div>
        <div class="currently">{{ selectItem?.notice }}</div>
      </div>
      <div class="item">
        <div class="title">创建时间:</div>
        <div class="currently">{{ formatDate(selectItem?.createTime) }}</div>
      </div>
    </div>
    <div class="menmber-list">
      <div style="font-size: 14px; color: #887b70; padding: 10px 0">群成员</div>
      <div class="list-li" v-for="(item, indexs) in enterpriseMembers" :key="indexs">
        <div class="label">
          <img
            src="@/assets/imgs/member.png"
            alt=""
            style="width: 20px; height: 20px; margin-right: 5px"
          />
          <span>{{ item.name }}</span>
        </div>
        <div class="content" :class="userTypeMap[item.userType]">
          {{ userTypeList[item.userType] }}
        </div>
      </div>
      <div style="font-size: 14px; color: #2bc04e; padding: 10px 0">外部成员</div>
      <div class="list-li" v-for="(item, indexs) in externalMembers" :key="indexs">
        <div class="label">
          <img
            src="@/assets/imgs/member2.png"
            alt=""
            style="width: 20px; height: 20px; margin-right: 5px"
          />
          <span>{{ item.name }}</span>
        </div>
        <div class="content">
          {{ userTypeList[item.userType] }}
        </div>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { formatDate } from '@/utils/formatTime'

const props = defineProps({
  selectItem: {
    type: Object
  },
  index: {
    type: String || Number
  },
  externalMembers: {
    type: Array
  },
  enterpriseMembers: {
    type: Array
  }
})

const divDomList = ref(new Map())
const userTypeList = {
  0: '',
  1: '管理员',
  2: '群主'
}
const userTypeMap = {
  1: 'warnMsg',
  2: 'errorMsg'
}
const ColorMap = ref([
  'rgb(132, 202, 176)',
  'rgb(188, 158, 221)',
  'rgb(125, 179, 217)',
  'rgb(140, 163, 205)'
])
const getDivDom = (el, item) => {
  if (el) {
    divDomList.value.set(item.chatId, el)
  }
}

watch(
  () => divDomList.value,
  (value) => {
    const canvas = value.get(props.selectItem.chatId)
    if (canvas) {
      let companyName = props.selectItem.name.slice(0, 2).toUpperCase() || '名称'
      let positLeft = 50
      const ctx = canvas.getContext('2d')
      ctx.scale(1, 0.5)
      ctx.font = '100px sans-serif'
      ctx.fillStyle = '#fff'
      ctx.fillText(companyName, positLeft, 180)
    }
  },
  { immediate: true, deep: true }
)
</script>
<style scoped lang="scss">
canvas {
  width: 40px;
  height: 40px;
  border-radius: 5px;
}
.chat-group-right {
  height: 100%;
  flex: 0.6;
  background-color: #f6f6f7;
  padding: 0 8px;
  overflow-y: auto;
  .user {
    padding: 16px 0;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    border-bottom: 1px solid #ececec;
    .name {
      max-width: 150px;
      min-width: 150px;
      margin-left: 10px;
      font-size: 16px;
      color: rgba(0, 0, 0, 0.65);
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}
.user-info {
  padding-top: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #ececec;
  font-size: 13px;
  color: #666;
  .title {
    width: 100px;
  }
  .currently {
    width: calc(100% - 100px);
    text-align: left;
    text-wrap: wrap;
  }
  .tel {
    margin-bottom: 10px;
    display: flex;
    .el-input {
      height: 17px;
    }
  }
  .item {
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .items {
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    .currently {
      height: auto !important;
    }
  }
}
.menmber-list {
  font-size: 13px;
  color: #666;
  .list-li {
    padding: 10px 0;
    border-bottom: 1px dotted #ececec;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .label {
      display: flex;
      align-items: center;
      flex: 3;
    }
    .content {
      flex: 1;
    }
    .footer {
      flex: 2;
    }
  }
}
.errorMsg {
  color: #f56c6c;
}
.warnMsg {
  color: #e6a23c;
}
</style>
